<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ results.evaluateCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ results.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            @click="curr(comm,index)"
            :class="{ active: active === index }"
            v-for="(comm, index) in results.tags"
            :key="comm.title"
            >{{ comm.title }}（{{ comm.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:;" :class="{active:sortInsex===index}" @click="sort(item,index)" v-for="(item,index) in sortList" :key="index" >{{item.name}}</a>
    </div>
    <div class="list">
      <div class="item" v-for="(item, index) in commentCount.items" :key="index">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ fliterName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i
              class="iconfont icon-wjx01"
              v-for="(value, index) in item.score"
              :key="index"
            ></i>
            <i
              class="iconfont icon-wjx02"
              v-for="(value, index) in 5 - item.score"
              :key="index"
            ></i>
            <span class="attr">{{ fliterArrt(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <GoodsCommentImage :pictures="item.pictures"/>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >

          </div>
        </div>
      </div>
      <XtxPagination :current-page="data.pages" :total="commentCount.counts" :btn="5" @pageTurner="pageTurner" v-if="commentCount.counts"/>
    </div>
  </div>
</template>
<script>
import { inject, reactive, ref, watch } from 'vue'
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/product'
import GoodsCommentImage from '../goods-comment-image.vue'
export default {
  components: { GoodsCommentImage },
  name: 'GoodsComment',
  setup () {
    const goods = inject('goods')
    const results = ref({})
    const active = ref(0)
    const commentCount = ref({})
    const sortInsex = ref(0)
    const data = reactive({
      pages: 1, // '是1页码'
      pageSize: '100', // 是10页尺寸
      hasPicture: null, // 否true是否有图
      tag: null, // 否质量好标签
      sortField: null, // 否praiseCount排序字段，可选值范围[praiseCount,createTime]
      sortMethod: null // 否desc排序方法，可选值范围[asc,desc],默认为desc
    })
    findGoodsCommentInfo(goods.value.id).then(({ result }) => {
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags
      ]
      goods.value.commentCount = result.evaluateCount
      results.value = result
    })

    watch(data, () => {
      findGoodsCommentList(goods.value.id, { data }).then(({ result }) => {
        commentCount.value = result
      })
    }, {
      immediate: true
    })
    const fliterName = (name) => {
      return name.slice(0, 1) + '***' + name.slice(-1)
    }
    const fliterArrt = (Arrt) => {
      let str = ' '
      Arrt.forEach((item) => {
        str += item.name + ': '
        str += item.nameValue + ' '
      })
      return str
    }
    const curr = (item, index) => {
      data.pages = 1
      active.value = index
      if (item.title === '全部评价') {
        data.hasPicture = null
        data.tag = null
      } else if (item.title === '有图') {
        data.hasPicture = true
        data.tag = null
      } else {
        data.hasPicture = null
        data.tag = item.title
      }
    }
    const sortList = [
      { name: '默认', value: 'null' },
      { name: '最新', value: 'praiseCount' },
      { name: '最热', value: 'createTime' }
    ]
    const sort = (item, index) => {
      data.pages = 1
      sortInsex.value = index
      data.sortField = item.value
    }
    const pageTurner = (page) => {
      data.pages = page
    }
    return { pageTurner, results, sort, sortInsex, sortList, active, data, curr, commentCount, fliterName, fliterArrt }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
        .text {
          color: #666;
          line-height: 24px;
        }
        .time {
          color: #999;
          display: flex;
          justify-content: space-between;
          margin-top: 5px;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
    }
  }
}
</style>
